<script setup lang="ts">
import { getUserPlanConfig } from '@/api/login/AuthApi';

import { showSuccessToast, showFailToast, showConfirmDialog } from 'vant';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
type ConfigVo = {
    key: string;
    config: string;
}
let configId = ref("");
let content = ref("");
let key = ref("");
let router = useRouter();

const getConfig = async () => {
    if (configId.value) {
        let vo: ConfigVo = await getUserPlanConfig(configId.value);
        
        content.value = vo.config;
        key.value = vo.key;

    }
    else {
        showFailToast("请联系管理员获取配置编码");
    }

};
const setConfig = () => {
    if (key.value && content.value) {
        showConfirmDialog({
            title: '提示',
            message: '此操作会覆盖原有配置，确认这样操作吗？',
            width: '80%'
        }).then(() => {
            localStorage.setItem(key.value, content.value);

            showSuccessToast("设置成功");
            let url = getMapUrl(key.value);
            if (url) {
                router.push({ path: url });
            }
        });
    }
    else{
        showFailToast("请联系管理员获取配置编码");
    }


};

const getMapUrl = (key: string) => {
    let url = '';
    switch (key) {
        case "BossRecmStrategy":
            url = "/plan/boss";
            break;
        case "BossRecmStrategyV2":
            url = "/plan/boss2.0";
            break;
        case "BossRecmStrategyV3NewVersion":
            url = "/plan/boss3.0";
            break;
        case "BossRecmStrategyV4":
            url = "/plan/boss4.0";
            break;
        case "BossRecmStrategyV5":
            url = "/plan/boss5.0";
            break;
        case "BossRecmStrategyV6":
            url = "/plan/boss6.0";
            break;
        default:
            showFailToast("暂不支持")
            break;
    }
    return url;
};
</script>
<template>
    <van-cell-group inset>
        <van-field v-model="configId" label="配置编码" placeholder="请输入配置编码" />
        <van-field v-model="content" rows="1" :autosize="{ maxHeight: 450, minHeight: 50 }" label="value"
            type="textarea" />
    </van-cell-group>
    <div style="margin: 16px;">
        <van-row gutter="20">
            <van-col :span="12">
                <van-button round block type="primary" native-type="submit" @click="getConfig">
                    获取配置
                </van-button>
            </van-col>
            <van-col :span="12">
                <van-button round block type="danger" native-type="submit" @click="setConfig">
                    设置配置
                </van-button>
            </van-col>
        </van-row>


    </div>
</template>